//
//  ST_FateTodayViewController.swift
//  btvideo
//
//  Created by mac on 2020/11/9.
//  Copyright © 2020 JarvanZhang. All rights reserved.
//

import UIKit
import SwiftyJSON
import SwiftyAttributes


class ST_FateTodayViewController: UIViewController {
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor = .clear
        view.addSubview(root_view)
        root_view.snp.makeConstraints { (make) in
            make.width.equalTo(300)
            make.height.equalTo(334)
            make.center.equalToSuperview()
        }
        
        root_view.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalToSuperview().offset(20)
            make.height.equalTo(28)
        }
        
        root_view.addSubview(content_label)
        content_label.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalToSuperview().offset(53)
        }
        
        
        root_view.addSubview(mid_content_view)
        mid_content_view.snp.makeConstraints { (make) in
            make.width.equalTo(83)
            make.height.equalTo(83+5+17)
            make.centerX.equalToSuperview()
            make.top.equalTo(content_label.snp.bottom).offset(20)
        }
        
        
        root_view.addSubview(left_content_view)
        left_content_view.snp.makeConstraints { (make) in
            make.top.width.height.equalTo(mid_content_view)
            make.right.equalTo(mid_content_view.snp.left).offset(-9)
        }
        
        
        root_view.addSubview(right_content_view)
        right_content_view.snp.makeConstraints { (make) in
            make.top.width.height.equalTo(mid_content_view)
            make.left.equalTo(mid_content_view.snp.right).offset(9)
        }
        
        
        root_view.addSubview(check_box_btn)
        check_box_btn.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(mid_content_view.snp.bottom).offset(15)
        }
        
        root_view.addSubview(confirm_gradient_view)
        root_view.addSubview(confirm_btn)
        
        confirm_gradient_view.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.bottom.equalToSuperview().offset(-20)
            make.width.equalTo(200)
            make.height.equalTo(44)
        }
        
        
        confirm_btn.snp.makeConstraints { (make) in
            make.edges.equalTo(confirm_gradient_view)
        }
        
        
        
        root_view.addSubview(close_btn)
        close_btn.snp.makeConstraints { (make) in
            make.width.height.equalTo(18)
            make.right.equalToSuperview().offset(-15)
            make.top.equalToSuperview().offset(15)
        }
        
        request_data()
    }
    
    fileprivate var data_array = [JSON]()
    
    
    fileprivate func request_data(){
        
        
        JVrequest(PH.todayMeetApi,success: { (json) in
            
            self.data_array = json["data"].arrayValue
            
            for (i,item) in [self.left_content_view,self.mid_content_view,self.right_content_view].enumerated(){
                
                guard let itemJson = self.data_array[safe:i] else { continue  }
                
                item.itemJson = itemJson
                
            }
            
        }, failure: { (code, err) in
            
            self.toast(text: err)
        })

        
    }
    
    
    //    MARK: - Properties
    fileprivate lazy var root_view:UIView = {
        let control = UIView()
        control.cornerRadius = 20
        control.backgroundColor = .white
        return control
    }()
    
    fileprivate lazy var titleLabel:UILabel = {
        let control = UILabel.init()
        control.text = "今日最佳缘分"
        control.textColor = UIColor(hexString: "#333333")
        control.textAlignment = .center
        control.font = UIFont.systemFont(ofSize: 20, weight: UIFont.Weight.medium)
        return control
    }()
    
    fileprivate lazy var content_string = "已为你选出对你感兴趣的今日最佳精选\n女生，马上去认识她们吧！"
    
    fileprivate lazy var content_label:UILabel = {
        
        let control = UILabel.init()
        
        control.numberOfLines = 0
        
        let  p = NSParagraphStyle.create_line_spacing_paragraph_style(lineSpacing: 3)
        
        p.alignment = .center
        
        control.attributedText = content_string.withAttributes([Attribute.font(UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.regular)),Attribute.textColor(UIColor.init(hex: 0x61666F)!),Attribute.paragraphStyle(p)])
        
        return control
    }()
    
    fileprivate lazy var mid_content_view:ST_FateTodayItemView = {
        let control = ST_FateTodayItemView()
        return control
    }()
    
    fileprivate lazy var left_content_view:ST_FateTodayItemView = {
        let control = ST_FateTodayItemView()
        return control
    }()
    
    fileprivate lazy var right_content_view:ST_FateTodayItemView = {
        let control = ST_FateTodayItemView()
        return control
    }()
    
    
    fileprivate lazy var check_box_btn:UIButton = {
        
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.titleLabel?.font = UIFont.systemFont(ofSize: 12, weight: UIFont.Weight.regular)
        control.setTitleForAllStates(" 今日不再弹框")
        control.setTitleColor(UIColor.init(hex: 0xACB4C0)!, for: UIControl.State.normal)
        control.setImage(ST_ImageUtils.imageName("checkboxround0")!, for: UIControl.State.normal)
        control.setImage(ST_ImageUtils.imageName("checkboxround1")!, for: UIControl.State.selected)
//        control.semanticContentAttribute = .forceRightToLeft
        control.addTarget(self, action: #selector(check_box_action), for: UIControl.Event.touchUpInside)
        return control
    }()
    
    fileprivate lazy var confirm_btn_string = "一键搭讪"
    
    fileprivate lazy var confirm_btn:UIButton = {
        let control = UIButton()
        control.setImage(ST_ImageUtils.imageName("搭讪icon"), for: UIControl.State.normal)
        control.setTitle(confirm_btn_string, for: UIControl.State.normal)
        control.titleEdgeInsets = UIEdgeInsets.init(top: 0, left: 5, bottom: 0, right: 0)
        control.setTitleColor(UIColor.white, for: UIControl.State.normal)
        control.titleLabel?.font = UIFont.systemFont(ofSize: 16, weight: UIFont.Weight.semibold)
        control.addTarget(self, action: #selector(accost_handle), for: UIControl.Event.touchUpInside)
        return control
    }()
    
    
    fileprivate lazy var confirm_gradient_view:ST_FWGradientView = {
        let control = ST_FWGradientView()
        control.bgLayer.colors = [UIColor.init(hex: 0xFFDE11)!.cgColor,UIColor.init(hex: 0xF8BC00)!.cgColor]
        control.cornerRadius = 22
        return control
    }()
    
    
    fileprivate lazy var close_btn:UIButton = {
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.setImageForAllStates(ST_ImageUtils.imageName("首页_icon_关闭")!)
        control.addTarget(self, action: #selector(close_handle), for: UIControl.Event.touchUpInside)
        return control
    }()
    
    
    deinit {
        print("严重的数据释放问题  0000001")
    }
}

extension ST_FateTodayViewController{
    
    
    @objc fileprivate func close_handle(){
        
        goback_previous_controller(animated: true) {
            ST_FateAlertManager.shared.dispatch()
        }
        
    }
    @objc fileprivate func check_box_action(){
        
        check_box_btn.isSelected =  !check_box_btn.isSelected
        
        
        if check_box_btn.isSelected{
            ST_FateAlertManager.shared.setCurrentAccountFateState(isFinish: true)
        }else{
            ST_FateAlertManager.shared.setCurrentAccountFateState(isFinish: false)
        }
        
    }
    
    @objc func accost_handle(){
        
        if BTVerticalAlertViewController.toastBindPhoneRequireAlert(fromController: self) {
            return
        }
        
        let array = self.data_array.map({$0["accid"].stringValue})
        
        ST_TianAccostManager.shared.accost(idSet: Set(array), viewController: self,success:  {[weak self] (json) in
       
            
            self?.toast(text: "搭讪成功")
            
            main_thread_execute(afterDelay: 2) {
                
                ST_FateAlertManager.shared.setCurrentAccountFateState(isFinish: true)
                
                self?.close_handle()
            }
            
            
        })
        
    }
    
}

//MARK: - Female Fate
//
class ST_FemaleFateTodayViewController: ST_FateTodayViewController {
  
    private lazy var _contentString: String = "已为你选出对你感兴趣的今日最佳精选\n男生，马上去认识他们吧！"

    override var content_string: String {
      get { return _contentString }
      set { _contentString = newValue }
    }
    
    private lazy var _confirmBtnString: String = "一键打招呼"

    override var confirm_btn_string: String {
      get { return _confirmBtnString }
      set { _confirmBtnString = newValue }
    }
    
    override func check_box_action() {
        
        check_box_btn.isSelected =  !check_box_btn.isSelected
        
        if check_box_btn.isSelected{
            ST_FateAlertManager.shared.setCurrentAccountFateState(isFinish: true)
        }else{
            ST_FateAlertManager.shared.setCurrentAccountFateState(isFinish: false)
        }
        
    }
    
    override func request_data() {
    
        JVrequest(PH.todayFemaleMeetApi,success: { (json) in
            
            self.data_array = json["data"].arrayValue
            
            for (i,item) in [self.left_content_view,self.mid_content_view,self.right_content_view].enumerated(){
                
                guard let itemJson = self.data_array[safe:i] else { continue  }
                
                item.itemJson = itemJson
                
            }
            
        }, failure: { (code, err) in
            
            self.toast(text: err)
        })

        
    }
    
    
    override func accost_handle() {
        
        if BTVerticalAlertViewController.toastBindPhoneRequireAlert(fromController: self) {
            return
        }
        
        ST_TianFemaleAccostManager.shared.accost(idSet: self.data_array, viewController: self) { (json) in
            
            self.toast(text: "打招呼成功")
            
            main_thread_execute(afterDelay: 2) {
                
                ST_FateAlertManager.shared.setCurrentAccountFateState(isFinish: true)
                
                self.goback_previous_controller()
            }
        }
    }
}

//MARK: - Item View
class ST_FateTodayItemView: UIControl {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.addSubview(avatar_image_view)
        avatar_image_view.snp.makeConstraints { (make) in
            make.top.left.right.equalToSuperview()
            make.height.equalTo(avatar_image_view.snp.width)
        }
        
        avatar_image_view.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { (make) in
            make.width.equalTo(34)
            make.height.equalTo(18)
            make.left.equalToSuperview().offset(5)
            make.bottom.equalToSuperview().offset(-5)
        }
        
        
        self.addSubview(content_label)
        content_label.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(avatar_image_view.snp.bottom).offset(5)
        }
        
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    fileprivate lazy var avatar_image_view:UIImageView = {
        let control = UIImageView()
        control.contentMode = .scaleAspectFill
        control.cornerRadius = 10
        return control
    }()
    
    
    fileprivate lazy var titleLabel:UILabel = {
        let control = UILabel.init()
        control.text = "真人"
        control.textColor = UIColor(hexString: "#333333")
        control.textAlignment = .center
        control.backgroundColor = .white
        control.cornerRadius = 9
        control.font = UIFont.systemFont(ofSize: 11, weight: UIFont.Weight.medium)
        return control
    }()
    
    
    fileprivate lazy var content_label:UILabel = {
        let control = UILabel.init()
        control.textColor = UIColor(hexString: "#333333")
        control.textAlignment = .center
        control.font = UIFont.systemFont(ofSize: 12, weight: UIFont.Weight.medium)
        return control
    }()
    
    var itemJson = JSON.null{
        didSet{
            
            content_label.text = itemJson["age"].intValue.string + "岁" + itemJson["distance"].stringValue
            
            avatar_image_view.pin_setImage(from: itemJson["avatar"].stringValue.compress_small_image(width: 83*3).url, placeholderImage: ST_SwiftConst.shared.fetch_random_place_holder_image())
        }
    }
    
}
